package lang;

import editor.NotImplementedException;
import model.code.*;

import java.io.IOException;
import java.io.PrintStream;

/**
 * This is the superclass of all language saving classes.
 *
 * @author Neil Dickson
 */
public abstract class Saver {
	public abstract void saveFile(CodeFile file,PrintStream output) throws IOException;
	public abstract void saveFunction(Function function,PrintStream output) throws IOException;
	public abstract void saveVariable(GlobalVariable variable,PrintStream output) throws IOException;
	public abstract void saveStructure(Structure structure,PrintStream output) throws IOException;
	public abstract void saveConstants(Constants constants,PrintStream output) throws IOException;
	public abstract void saveSection(Section section,PrintStream output) throws IOException;
	public abstract void savePrototypes(Prototypes prototypes,PrintStream output) throws IOException;
	public abstract void savePreprocessorLine(PreprocessorLine line,PrintStream output) throws IOException;

	public void saveFile(CodeFile file) throws IOException {
		PrintStream stream = new PrintStream(file.getFilePath());
		saveFile(file,stream);
		stream.close();
	}
	public void saveChunk(CodeFileChunk chunk,PrintStream output) throws IOException {
		if (chunk instanceof Function) {
			saveFunction((Function)chunk,output);
		}
		else if (chunk instanceof GlobalVariable) {
			saveVariable((GlobalVariable)chunk,output);
		}
		else if (chunk instanceof Structure) {
			saveStructure((Structure)chunk,output);
		}
		else if (chunk instanceof Constants) {
			saveConstants((Constants)chunk,output);
		}
		else if (chunk instanceof CodeFile) {
			saveFile((CodeFile)chunk,output);
		}
		else if (chunk instanceof Section) {
			saveSection((Section)chunk,output);
		}
		else if (chunk instanceof Prototypes) {
			savePrototypes((Prototypes)chunk,output);
		}
		else if (chunk instanceof PreprocessorLine) {
			savePreprocessorLine((PreprocessorLine)chunk,output);
		}
		else {
			throw new NotImplementedException("Error! Unknown subclass of CodeFileChunk: "+chunk.getClass().toString());
		}
	}
}
